package com.apes.scm.rbac.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.scm.rbac.multiKeys.MenuOperationKey;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：操作
 *
 * @author xul
 * @create 2018-01-27 11:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Operation.CONST_TABLE_NAME)
@IdClass(MenuOperationKey.class)
public class Operation extends BaseModel {
    public static final String CONST_TABLE_NAME = "aut_operations";

    /**
     * 菜单
     **/
    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name = "menu_id", referencedColumnName = "id", nullable = false)
    private Menu menu;

    /**
     * 操作ID
     **/
    @Id
    @Column(name = "operation_id", length = 60)
    private String operationId;

    /**
     * 操作名称
     **/
    @Column(name = "name", length = 60, nullable = false)
    private String name;

    /**
     * 接口编号
     **/
    @Column(name = "api", length = 60)
    private String api;

    /**
     * 打印模板
     **/
    @Column(name = "report")
    private String report;

    @ManyToMany(cascade = {CascadeType.MERGE},mappedBy = "operations", fetch = FetchType.LAZY)
    private Set<Role> roles = new HashSet();

    @Transient
    private String id;

    @Transient
    private String menuName;




//    public String getId() {
//        return  menu.getId() + "," + getOperationId();
//    }

//    @Transient
//    private Map menuCopy;
//
//    public Map getMenuCopy() {
//        Map menuMap = new HashMap();
//        menuMap.put("id",menu.getId());
//        menuMap.put("name",menu.getName());
//        menuMap.put("sortCode",menu.getSortCode());
//        return menuMap;
//    }

}